package com.realbrand.net.realbrandpay.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.realbrand.net.realbrandpay.R;
import com.realbrand.net.realbrandpay.bean.AllOrderBean;
import com.realbrand.net.realbrandpay.bean.OrderFragmentBean;
import com.realbrand.net.realbrandpay.zxing.util.L;
import com.squareup.picasso.Picasso;

import java.text.DecimalFormat;
import java.util.List;

/**
 * Created by Administrator on 2018/5/18.
 */

public class OrderGroupAdapter extends BaseAdapter {
    private Context mContext;
    private LayoutInflater inflater;
    private AllOrderBean allOrderBean;

    public OrderGroupAdapter(AllOrderBean allOrderBean, Context context) {
        this.allOrderBean = allOrderBean;
        this.mContext = context;
        inflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
        return allOrderBean.getList().size();
    }

    @Override
    public Object getItem(int position) {
        return allOrderBean.getList().get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        OrderGroupViewHolder groupViewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.orderfragment_lv_item_layout, parent, false);
            groupViewHolder = new OrderGroupViewHolder(convertView);
            convertView.setTag(groupViewHolder);
        } else {
            groupViewHolder = (OrderGroupViewHolder) convertView.getTag();
        }
        groupViewHolder.dealNumber.setText(allOrderBean.getList().get(position).getDealNumber());
        double allMoney = 0.00;
        for (int i = 0; i < allOrderBean.getList().get(position).getList().size(); i++) {
            allMoney += Double.parseDouble(allOrderBean.getList().get(position).getList().get(i).getCommodityPrice()) * Double.parseDouble(allOrderBean.getList().get(position).getList().get(i).getCommoditySum());
        }
        groupViewHolder.allMoney.setText(new DecimalFormat("0.00").format(allMoney));
        OrderChildAdapter childAdapter = new OrderChildAdapter(mContext, allOrderBean.getList().get(position).getList());
        L.d(L.TAG,"传到childAdapter的数据"+allOrderBean.getList().get(position).getList().toString());
        groupViewHolder.groupLv.setAdapter(childAdapter);
        setListViewHeightBasedOnChildren(groupViewHolder.groupLv);

        return convertView;
    }

    class OrderGroupViewHolder {
        private TextView dealNumber;
        private TextView allMoney;
        private ListView groupLv;

        public OrderGroupViewHolder(View view) {
            dealNumber = (TextView) view.findViewById(R.id.orderFragment_tv_DealNumber);
            allMoney = (TextView) view.findViewById(R.id.orderFragment_commidityAllMoney);
            groupLv = (ListView) view.findViewById(R.id.orderFragment_child_lv);
        }
    }
    public void setListViewHeightBasedOnChildren(ListView listView) {
// 获取ListView对应的Adapter
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }
        int totalHeight = 0;
        for (int i = 0, len = listAdapter.getCount(); i < len; i++) {
            // listAdapter.getCount()返回数据项的数目
            View listItem = listAdapter.getView(i, null, listView);
            // 计算子项View 的宽高
            listItem.measure(0, 0);
// 统计所有子项的总高度
            totalHeight += listItem.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() *
                (listAdapter.getCount() - 1));
// listView.getDividerHeight()获取子项间分隔符占用的高度
// params.height最后得到整个ListView完整显示需要的高度
        listView.setLayoutParams(params);
    }
}
